package com.mypj.base.service.impl;

import com.mypj.base.entity.po.SysUser;
import com.mypj.base.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = loginService.login(username);
        List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>() {
            {
                add(new SimpleGrantedAuthority("admin"));
            }
        };
        return new User(user.getUsername(), user.getPassword(), authorityList);
    }
}
